//
//  Dot3.m
//  Techdemo
//
//  Created by Sidney Just on 24.02.10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "pixelation.h"


@implementation pixelation
@synthesize pixelationX, pixelationY;

- (void)bindAttributeLocations {
#ifdef ALLOW_ES2
	glBindAttribLocation(shader, 0, "a_position"); // Bind the a_position attribute
	glBindAttribLocation(shader, 1, "a_texCoord"); // Bind the a_texCoord attribute
#endif
}

- (void)getUniformLocations {
#ifdef ALLOW_ES2
	// Get Attribute locations
	positionLoc = glGetAttribLocation(shader, "a_position");
	texCoordLoc = glGetAttribLocation(shader, "a_texCoord");
	
	// Get Uniform locations
	samplerLoc		= glGetUniformLocation(shader, "s_texture");
	pixelationLoc	= glGetUniformLocation(shader, "v_pixelation");
	matrixLoc		= glGetUniformLocation(shader, "modelViewProjectionMatrix");
#endif
}

- (void)runShaderFFP {
	glColor4f(color->red, color->green, color->blue, color->alpha);
	
	glBindTexture(GL_TEXTURE_2D, texture.texture);
	vdEnable(GL_TEXTURE_2D);
	
	glLoadIdentity();
	glVertexPointer(positionsPerVertex, GL_FLOAT, 0, vertices);
	vdEnableClientState(GL_VERTEX_ARRAY);
	glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
}

- (void)runShader {
#ifdef ALLOW_ES2
	// Projection matrix
	GLfloat proj[16];
	if(!isPostProcessingShader)
	{
		mat4f_LoadProjection(proj); // Load the projection matrix
	} else {
		mat4f_LoadProjectionWithoutRotation(proj); // Load the projection matrix without rotation
	}

	glUniformMatrix4fv(matrixLoc, 1, GL_FALSE, proj); // Send the projection matrix to the shader
	
	// Texture
	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D, texture.texture); // Bind the texture
	glUniform1i(samplerLoc, 0); // Send the texture to the texture sampler uniform
	
	// Vertices
	glVertexAttribPointer(positionLoc, 2, GL_FLOAT, GL_FALSE, 0, vertices); // Send the vertices to the shader
	glEnableVertexAttribArray(positionLoc);
	
	glVertexAttribPointer(texCoordLoc, 2, GL_FLOAT, GL_FALSE, 0, texCoords); // Send the texture coordinates to the shader
	glEnableVertexAttribArray(texCoordLoc);
	
	glUniform2f(pixelationLoc, pixelationX, pixelationY);
#endif
}

- (id)init {
	if(self = [super init])
	{
		pixelationX = 100.0f;
		pixelationY = 100.0f;
		[self loadShader:@"pixelationShader"];
	}
	return self;
}

- (void)dealloc {
	[super dealloc];
}

@end
